import Taro, { Component } from '@tarojs/taro'
import { AtIcon } from 'taro-ui'
import classnames from 'classnames'
import PropsType from 'prop-types'
import { View, Text } from '@tarojs/components'
import '../styles/agree.scss'

export default class DAgree extends Component {

    constructor(props) {
        super(props)
        this.state = {
            status: false
        }
    }

    componentWillMount() {
        let { value } = this.props
        this.setState({
            status: value
        })
    }

    onHandleAgree(e) {
        e.stopPropagation()
        const { type, text } = this.props
        if (type === '1') {
            Taro.navigateTo({
                url: '/pages/earnestpro/earnestpro'
            })
        } else if (type === '2') {
            Taro.navigateTo({
                url: "/pages/selfprotocal/selfprotocal"
            })
        } else if (type === '3') {
            Taro.navigateTo({
                url: '/pages/entprotocal/entprotocal'
            })
        }
        else if (type === '4') {
            Taro.navigateTo({
                url: '/pages/transpro/transpro'
            })
        } else if (type === '5') {
            Taro.navigateTo({
                url: '/pages/purchasepro/purchasepro'
            })
        } else {
            return Taro.showToast({
                title: '当前' + text + '正在完善中!',
                icon: "none"
            })
        }
    }

    onHandleChange(e) {
        e.stopPropagation()
        let { status } = this.state
        status = !status
        this.setState({
            status
        }, () => {
            this.props.onHandleChange && this.props.onHandleChange(this.state.status)
        })
    }

    render() {
        const {
            status
        } = this.state
        const {
            className,
            text
        } = this.props
        return (
            <View className={classnames('b_agree', className)} onClick={this.onHandleChange.bind(this)}>
                <View className={classnames('b_agree__box', status ? 'b_agree__bc' : '')}>
                    {
                        status && <AtIcon className='b_agree__box_icon' value='check' size='10' color='#ffffff'></AtIcon>
                    }
                </View>
                <View className='b_agree__text'>
                    <Text>已阅读并同意</Text>
                    <Text className='b_agree__text_agree' onClick={this.onHandleAgree.bind(this)}>{text}</Text>
                </View>
            </View>
        )
    }
}

DAgree.defaultProps = {
    className: '',
    text: '车辆运输服务',
    value: true,
    type: '',
    onHandleChange: () => { }
}

DAgree.PropsType = {
    className: PropsType.oneOfType[PropsType.string, PropsType.object],
    text: PropsType.string,
    value: PropsType.bool,
    type: PropsType.string,
    onHandleChange: PropsType.func
}
